ncnv.pas 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860
  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. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1350. include(left.flags, nf_typedaddr);
  1351. result:=left;
  1352. left:=nil;
  1353. exit;
  1354. end;
  1355. end;
  1356. end;
  1357. te_convert_l1,
  1358. te_convert_l2,
  1359. te_convert_l3 :
  1360. begin
  1361. result := simplify;
  1362. if assigned(result) then
  1363. exit;
  1364. { nothing to do }
  1365. end;
  1366. te_convert_operator :
  1367. begin
  1368. include(current_procinfo.flags,pi_do_call);
  1369. inc(aprocdef.procsym.refs);
  1370. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1371. { tell explicitly which def we must use !! (PM) }
  1372. tcallnode(hp).procdefinition:=aprocdef;
  1373. left:=nil;
  1374. result:=hp;
  1375. exit;
  1376. end;
  1377. te_incompatible :
  1378. begin
  1379. { Procedures have a resulttype.def of voiddef and functions of their
  1380. own resulttype.def. They will therefore always be incompatible with
  1381. a procvar. Because isconvertable cannot check for procedures we
  1382. use an extra check for them.}
  1383. if (left.nodetype=calln) and
  1384. (tcallnode(left).para_count=0) and
  1385. (resulttype.def.deftype=procvardef) and
  1386. (
  1387. (m_tp_procvar in aktmodeswitches) or
  1388. (m_mac_procvar in aktmodeswitches)
  1389. ) then
  1390. begin
  1391. if assigned(tcallnode(left).right) then
  1392. begin
  1393. { this is already a procvar, if it is really equal
  1394. is checked below }
  1395. convtype:=tc_equal;
  1396. hp:=tcallnode(left).right.getcopy;
  1397. currprocdef:=tabstractprocdef(hp.resulttype.def);
  1398. end
  1399. else
  1400. begin
  1401. convtype:=tc_proc_2_procvar;
  1402. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1403. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1404. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1405. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1406. begin
  1407. if assigned(tcallnode(left).methodpointer) then
  1408. tloadnode(hp).set_mp(tcallnode(left).get_load_methodpointer)
  1409. else
  1410. tloadnode(hp).set_mp(load_self_node);
  1411. end;
  1412. resulttypepass(hp);
  1413. end;
  1414. left.free;
  1415. left:=hp;
  1416. { Now check if the procedure we are going to assign to
  1417. the procvar, is compatible with the procvar's type }
  1418. if not(nf_explicit in flags) and
  1419. (proc_to_procvar_equal(currprocdef,tprocvardef(resulttype.def))=te_incompatible) then
  1420. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1421. exit;
  1422. end;
  1423. { Handle explicit type conversions }
  1424. if nf_explicit in flags then
  1425. begin
  1426. { do common tc_equal cast }
  1427. convtype:=tc_equal;
  1428. { ordinal constants can be resized to 1,2,4,8 bytes }
  1429. if (left.nodetype=ordconstn) then
  1430. begin
  1431. { Insert typeconv for ordinal to the correct size first on left, after
  1432. that the other conversion can be done }
  1433. htype.reset;
  1434. case longint(resulttype.def.size) of
  1435. 1 :
  1436. htype:=s8inttype;
  1437. 2 :
  1438. htype:=s16inttype;
  1439. 4 :
  1440. htype:=s32inttype;
  1441. 8 :
  1442. htype:=s64inttype;
  1443. end;
  1444. { we need explicit, because it can also be an enum }
  1445. if assigned(htype.def) then
  1446. inserttypeconv_internal(left,htype)
  1447. else
  1448. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1449. end;
  1450. { check if the result could be in a register }
  1451. if (not(tstoreddef(resulttype.def).is_intregable) and
  1452. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1453. ((left.resulttype.def.deftype = floatdef) and
  1454. (resulttype.def.deftype <> floatdef)) then
  1455. make_not_regable(left,vr_addr);
  1456. { class/interface to class/interface, with checkobject support }
  1457. if is_class_or_interface(resulttype.def) and
  1458. is_class_or_interface(left.resulttype.def) then
  1459. begin
  1460. { check if the types are related }
  1461. if not(nf_internal in flags) and
  1462. (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1463. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1464. begin
  1465. { Give an error when typecasting class to interface, this is compatible
  1466. with delphi }
  1467. if is_interface(resulttype.def) and
  1468. not is_interface(left.resulttype.def) then
  1469. CGMessage2(type_e_classes_not_related,
  1470. FullTypeName(left.resulttype.def,resulttype.def),
  1471. FullTypeName(resulttype.def,left.resulttype.def))
  1472. else
  1473. CGMessage2(type_w_classes_not_related,
  1474. FullTypeName(left.resulttype.def,resulttype.def),
  1475. FullTypeName(resulttype.def,left.resulttype.def))
  1476. end;
  1477. { Add runtime check? }
  1478. if (cs_check_object in aktlocalswitches) then
  1479. begin
  1480. { we can translate the typeconvnode to 'as' when
  1481. typecasting to a class or interface }
  1482. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1483. left:=nil;
  1484. result:=hp;
  1485. exit;
  1486. end;
  1487. end
  1488. else
  1489. begin
  1490. { only if the same size or formal def }
  1491. if not(
  1492. (left.resulttype.def.deftype=formaldef) or
  1493. (
  1494. not(is_open_array(left.resulttype.def)) and
  1495. not(is_array_constructor(left.resulttype.def)) and
  1496. (left.resulttype.def.size=resulttype.def.size)
  1497. ) or
  1498. (
  1499. is_void(left.resulttype.def) and
  1500. (left.nodetype=derefn)
  1501. )
  1502. ) then
  1503. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1504. end;
  1505. end
  1506. else
  1507. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1508. end;
  1509. else
  1510. internalerror(200211231);
  1511. end;
  1512. end;
  1513. { Give hint or warning for unportable code, exceptions are
  1514. - typecasts from constants
  1515. - void }
  1516. if not(nf_internal in flags) and
  1517. (left.nodetype<>ordconstn) and
  1518. not(is_void(left.resulttype.def)) and
  1519. (((left.resulttype.def.deftype=orddef) and
  1520. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1521. ((resulttype.def.deftype=orddef) and
  1522. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef]))) then
  1523. begin
  1524. { Give a warning when sizes don't match, because then info will be lost }
  1525. if left.resulttype.def.size=resulttype.def.size then
  1526. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1527. else
  1528. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1529. end;
  1530. result := simplify;
  1531. if assigned(result) then
  1532. exit;
  1533. { now call the resulttype helper to do constant folding }
  1534. result:=resulttype_call_helper(convtype);
  1535. end;
  1536. function ttypeconvnode.simplify: tnode;
  1537. var
  1538. hp: tnode;
  1539. begin
  1540. result := nil;
  1541. { Constant folding and other node transitions to
  1542. remove the typeconv node }
  1543. case left.nodetype of
  1544. realconstn :
  1545. begin
  1546. if (convtype = tc_real_2_currency) then
  1547. result := resulttype_real_to_currency
  1548. else if (convtype = tc_real_2_real) then
  1549. result := resulttype_real_to_real
  1550. else
  1551. exit;
  1552. if not(assigned(result)) then
  1553. begin
  1554. result := left;
  1555. left := nil;
  1556. end;
  1557. if (result.nodetype = realconstn) then
  1558. begin
  1559. result:=crealconstnode.create(trealconstnode(result).value_real,resulttype);
  1560. if ([nf_explicit,nf_internal] * flags <> []) then
  1561. include(result.flags, nf_explicit);
  1562. end;
  1563. end;
  1564. niln :
  1565. begin
  1566. { nil to ordinal node }
  1567. if (resulttype.def.deftype=orddef) then
  1568. begin
  1569. hp:=cordconstnode.create(0,resulttype,true);
  1570. if ([nf_explicit,nf_internal] * flags <> []) then
  1571. include(hp.flags, nf_explicit);
  1572. result:=hp;
  1573. exit;
  1574. end
  1575. else
  1576. { fold nil to any pointer type }
  1577. if (resulttype.def.deftype=pointerdef) then
  1578. begin
  1579. hp:=cnilnode.create;
  1580. hp.resulttype:=resulttype;
  1581. if ([nf_explicit,nf_internal] * flags <> []) then
  1582. include(hp.flags, nf_explicit);
  1583. result:=hp;
  1584. exit;
  1585. end
  1586. else
  1587. { remove typeconv after niln, but not when the result is a
  1588. methodpointer. The typeconv of the methodpointer will then
  1589. take care of updateing size of niln to OS_64 }
  1590. if not((resulttype.def.deftype=procvardef) and
  1591. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1592. begin
  1593. left.resulttype:=resulttype;
  1594. if ([nf_explicit,nf_internal] * flags <> []) then
  1595. include(left.flags, nf_explicit);
  1596. result:=left;
  1597. left:=nil;
  1598. exit;
  1599. end;
  1600. end;
  1601. ordconstn :
  1602. begin
  1603. { ordinal contants can be directly converted }
  1604. { but not char to char because it is a widechar to char or via versa }
  1605. { which needs extra code to do the code page transistion }
  1606. { constant ordinal to pointer }
  1607. if (resulttype.def.deftype=pointerdef) and
  1608. (convtype<>tc_cchar_2_pchar) then
  1609. begin
  1610. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  1611. if ([nf_explicit,nf_internal] * flags <> []) then
  1612. include(hp.flags, nf_explicit);
  1613. result:=hp;
  1614. exit;
  1615. end
  1616. else if is_ordinal(resulttype.def) and
  1617. not(convtype=tc_char_2_char) then
  1618. begin
  1619. { replace the resulttype and recheck the range }
  1620. left.resulttype:=resulttype;
  1621. if ([nf_explicit,nf_internal] * flags <> []) then
  1622. include(left.flags, nf_explicit);
  1623. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1624. result:=left;
  1625. left:=nil;
  1626. exit;
  1627. end;
  1628. end;
  1629. pointerconstn :
  1630. begin
  1631. { pointerconstn to any pointer is folded too }
  1632. if (resulttype.def.deftype=pointerdef) then
  1633. begin
  1634. left.resulttype:=resulttype;
  1635. if ([nf_explicit,nf_internal] * flags <> []) then
  1636. include(left.flags, nf_explicit);
  1637. result:=left;
  1638. left:=nil;
  1639. exit;
  1640. end
  1641. { constant pointer to ordinal }
  1642. else if is_ordinal(resulttype.def) then
  1643. begin
  1644. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1645. resulttype,not(nf_explicit in flags));
  1646. if ([nf_explicit,nf_internal] * flags <> []) then
  1647. include(hp.flags, nf_explicit);
  1648. result:=hp;
  1649. exit;
  1650. end;
  1651. end;
  1652. end;
  1653. end;
  1654. procedure Ttypeconvnode.mark_write;
  1655. begin
  1656. left.mark_write;
  1657. end;
  1658. function ttypeconvnode.first_cord_to_pointer : tnode;
  1659. begin
  1660. result:=nil;
  1661. internalerror(200104043);
  1662. end;
  1663. function ttypeconvnode.first_int_to_int : tnode;
  1664. begin
  1665. first_int_to_int:=nil;
  1666. expectloc:=left.expectloc;
  1667. if not is_void(left.resulttype.def) then
  1668. begin
  1669. if (left.expectloc<>LOC_REGISTER) and
  1670. (resulttype.def.size>left.resulttype.def.size) then
  1671. expectloc:=LOC_REGISTER
  1672. else
  1673. if (left.expectloc=LOC_CREGISTER) and
  1674. (resulttype.def.size<left.resulttype.def.size) then
  1675. expectloc:=LOC_REGISTER;
  1676. end;
  1677. {$ifndef cpu64bit}
  1678. if is_64bit(resulttype.def) then
  1679. registersint:=max(registersint,2)
  1680. else
  1681. {$endif cpu64bit}
  1682. registersint:=max(registersint,1);
  1683. end;
  1684. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1685. begin
  1686. result:=nil;
  1687. registersint:=1;
  1688. expectloc:=LOC_REGISTER;
  1689. end;
  1690. function ttypeconvnode.first_cstring_to_int : tnode;
  1691. begin
  1692. result:=nil;
  1693. internalerror(200510014);
  1694. end;
  1695. function ttypeconvnode.first_string_to_chararray : tnode;
  1696. begin
  1697. first_string_to_chararray:=nil;
  1698. expectloc:=left.expectloc;
  1699. end;
  1700. function ttypeconvnode.first_char_to_string : tnode;
  1701. begin
  1702. first_char_to_string:=nil;
  1703. expectloc:=LOC_REFERENCE;
  1704. end;
  1705. function ttypeconvnode.first_nothing : tnode;
  1706. begin
  1707. first_nothing:=nil;
  1708. end;
  1709. function ttypeconvnode.first_array_to_pointer : tnode;
  1710. begin
  1711. first_array_to_pointer:=nil;
  1712. if registersint<1 then
  1713. registersint:=1;
  1714. expectloc:=LOC_REGISTER;
  1715. end;
  1716. function ttypeconvnode.first_int_to_real: tnode;
  1717. var
  1718. fname: string[32];
  1719. typname : string[12];
  1720. begin
  1721. { Get the type name }
  1722. { Normally the typename should be one of the following:
  1723. single, double - carl
  1724. }
  1725. typname := lower(pbestrealtype^.def.gettypename);
  1726. { converting a 64bit integer to a float requires a helper }
  1727. if is_64bit(left.resulttype.def) then
  1728. begin
  1729. if is_signed(left.resulttype.def) then
  1730. fname := 'fpc_int64_to_'+typname
  1731. else
  1732. {$warning generic conversion from int to float does not support unsigned integers}
  1733. fname := 'fpc_int64_to_'+typname;
  1734. result := ccallnode.createintern(fname,ccallparanode.create(
  1735. left,nil));
  1736. left:=nil;
  1737. firstpass(result);
  1738. exit;
  1739. end
  1740. else
  1741. { other integers are supposed to be 32 bit }
  1742. begin
  1743. {$warning generic conversion from int to float does not support unsigned integers}
  1744. if is_signed(left.resulttype.def) then
  1745. fname := 'fpc_longint_to_'+typname
  1746. else
  1747. fname := 'fpc_longint_to_'+typname;
  1748. result := ccallnode.createintern(fname,ccallparanode.create(
  1749. left,nil));
  1750. left:=nil;
  1751. firstpass(result);
  1752. exit;
  1753. end;
  1754. end;
  1755. function ttypeconvnode.first_real_to_real : tnode;
  1756. begin
  1757. {$ifdef cpufpemu}
  1758. if cs_fp_emulation in aktmoduleswitches then
  1759. begin
  1760. if target_info.system in system_wince then
  1761. begin
  1762. case tfloatdef(left.resulttype.def).typ of
  1763. s32real:
  1764. case tfloatdef(resulttype.def).typ of
  1765. s64real:
  1766. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  1767. s32real:
  1768. begin
  1769. result:=left;
  1770. left:=nil;
  1771. end;
  1772. else
  1773. internalerror(2005082704);
  1774. end;
  1775. s64real:
  1776. case tfloatdef(resulttype.def).typ of
  1777. s32real:
  1778. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  1779. s64real:
  1780. begin
  1781. result:=left;
  1782. left:=nil;
  1783. end;
  1784. else
  1785. internalerror(2005082703);
  1786. end;
  1787. else
  1788. internalerror(2005082702);
  1789. end;
  1790. left:=nil;
  1791. firstpass(result);
  1792. exit;
  1793. end
  1794. else
  1795. begin
  1796. case tfloatdef(left.resulttype.def).typ of
  1797. s32real:
  1798. case tfloatdef(resulttype.def).typ of
  1799. s64real:
  1800. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  1801. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').restype),nil)),resulttype);
  1802. s32real:
  1803. begin
  1804. result:=left;
  1805. left:=nil;
  1806. end;
  1807. else
  1808. internalerror(200610151);
  1809. end;
  1810. s64real:
  1811. case tfloatdef(resulttype.def).typ of
  1812. s32real:
  1813. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  1814. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').restype),nil)),resulttype);
  1815. s64real:
  1816. begin
  1817. result:=left;
  1818. left:=nil;
  1819. end;
  1820. else
  1821. internalerror(200610152);
  1822. end;
  1823. else
  1824. internalerror(200610153);
  1825. end;
  1826. left:=nil;
  1827. firstpass(result);
  1828. exit;
  1829. end;
  1830. end
  1831. else
  1832. {$endif cpufpemu}
  1833. begin
  1834. first_real_to_real:=nil;
  1835. if registersfpu<1 then
  1836. registersfpu:=1;
  1837. expectloc:=LOC_FPUREGISTER;
  1838. end;
  1839. end;
  1840. function ttypeconvnode.first_pointer_to_array : tnode;
  1841. begin
  1842. first_pointer_to_array:=nil;
  1843. if registersint<1 then
  1844. registersint:=1;
  1845. expectloc:=LOC_REFERENCE;
  1846. end;
  1847. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1848. begin
  1849. first_cchar_to_pchar:=nil;
  1850. internalerror(200104021);
  1851. end;
  1852. function ttypeconvnode.first_bool_to_int : tnode;
  1853. begin
  1854. first_bool_to_int:=nil;
  1855. { byte(boolean) or word(wordbool) or longint(longbool) must
  1856. be accepted for var parameters }
  1857. if (nf_explicit in flags) and
  1858. (left.resulttype.def.size=resulttype.def.size) and
  1859. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1860. exit;
  1861. { when converting to 64bit, first convert to a 32bit int and then }
  1862. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1863. if resulttype.def.size > sizeof(aint) then
  1864. begin
  1865. result := ctypeconvnode.create_internal(left,u32inttype);
  1866. result := ctypeconvnode.create(result,resulttype);
  1867. left := nil;
  1868. firstpass(result);
  1869. exit;
  1870. end;
  1871. expectloc:=LOC_REGISTER;
  1872. if registersint<1 then
  1873. registersint:=1;
  1874. end;
  1875. function ttypeconvnode.first_int_to_bool : tnode;
  1876. begin
  1877. first_int_to_bool:=nil;
  1878. { byte(boolean) or word(wordbool) or longint(longbool) must
  1879. be accepted for var parameters }
  1880. if (nf_explicit in flags) and
  1881. (left.resulttype.def.size=resulttype.def.size) and
  1882. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1883. exit;
  1884. expectloc:=LOC_REGISTER;
  1885. { need if bool to bool !!
  1886. not very nice !!
  1887. insertypeconv(left,s32inttype);
  1888. left.explizit:=true;
  1889. firstpass(left); }
  1890. if registersint<1 then
  1891. registersint:=1;
  1892. end;
  1893. function ttypeconvnode.first_bool_to_bool : tnode;
  1894. begin
  1895. first_bool_to_bool:=nil;
  1896. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  1897. expectloc := left.expectloc
  1898. else
  1899. begin
  1900. expectloc:=LOC_REGISTER;
  1901. if registersint<1 then
  1902. registersint:=1;
  1903. end;
  1904. end;
  1905. function ttypeconvnode.first_char_to_char : tnode;
  1906. begin
  1907. first_char_to_char:=first_int_to_int;
  1908. end;
  1909. function ttypeconvnode.first_proc_to_procvar : tnode;
  1910. begin
  1911. first_proc_to_procvar:=nil;
  1912. if tabstractprocdef(resulttype.def).is_addressonly then
  1913. begin
  1914. registersint:=left.registersint;
  1915. if registersint<1 then
  1916. registersint:=1;
  1917. expectloc:=LOC_REGISTER;
  1918. end
  1919. else
  1920. begin
  1921. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  1922. CGMessage(parser_e_illegal_expression);
  1923. registersint:=left.registersint;
  1924. expectloc:=left.expectloc
  1925. end
  1926. end;
  1927. function ttypeconvnode.first_load_smallset : tnode;
  1928. var
  1929. srsym: ttypesym;
  1930. p: tcallparanode;
  1931. begin
  1932. srsym:=search_system_type('FPC_SMALL_SET');
  1933. p := ccallparanode.create(left,nil);
  1934. { reused }
  1935. left := nil;
  1936. { convert parameter explicitely to fpc_small_set }
  1937. p.left := ctypeconvnode.create_internal(p.left,srsym.restype);
  1938. { create call, adjust resulttype }
  1939. result :=
  1940. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1941. firstpass(result);
  1942. end;
  1943. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1944. begin
  1945. first_ansistring_to_pchar:=nil;
  1946. expectloc:=LOC_REGISTER;
  1947. if registersint<1 then
  1948. registersint:=1;
  1949. end;
  1950. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1951. begin
  1952. first_arrayconstructor_to_set:=nil;
  1953. internalerror(200104022);
  1954. end;
  1955. function ttypeconvnode.first_class_to_intf : tnode;
  1956. begin
  1957. first_class_to_intf:=nil;
  1958. expectloc:=LOC_REGISTER;
  1959. if registersint<1 then
  1960. registersint:=1;
  1961. end;
  1962. function ttypeconvnode._first_int_to_int : tnode;
  1963. begin
  1964. result:=first_int_to_int;
  1965. end;
  1966. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1967. begin
  1968. result:=first_cstring_to_pchar;
  1969. end;
  1970. function ttypeconvnode._first_cstring_to_int : tnode;
  1971. begin
  1972. result:=first_cstring_to_int;
  1973. end;
  1974. function ttypeconvnode._first_string_to_chararray : tnode;
  1975. begin
  1976. result:=first_string_to_chararray;
  1977. end;
  1978. function ttypeconvnode._first_char_to_string : tnode;
  1979. begin
  1980. result:=first_char_to_string;
  1981. end;
  1982. function ttypeconvnode._first_nothing : tnode;
  1983. begin
  1984. result:=first_nothing;
  1985. end;
  1986. function ttypeconvnode._first_array_to_pointer : tnode;
  1987. begin
  1988. result:=first_array_to_pointer;
  1989. end;
  1990. function ttypeconvnode._first_int_to_real : tnode;
  1991. begin
  1992. result:=first_int_to_real;
  1993. end;
  1994. function ttypeconvnode._first_real_to_real : tnode;
  1995. begin
  1996. result:=first_real_to_real;
  1997. end;
  1998. function ttypeconvnode._first_pointer_to_array : tnode;
  1999. begin
  2000. result:=first_pointer_to_array;
  2001. end;
  2002. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2003. begin
  2004. result:=first_cchar_to_pchar;
  2005. end;
  2006. function ttypeconvnode._first_bool_to_int : tnode;
  2007. begin
  2008. result:=first_bool_to_int;
  2009. end;
  2010. function ttypeconvnode._first_int_to_bool : tnode;
  2011. begin
  2012. result:=first_int_to_bool;
  2013. end;
  2014. function ttypeconvnode._first_bool_to_bool : tnode;
  2015. begin
  2016. result:=first_bool_to_bool;
  2017. end;
  2018. function ttypeconvnode._first_proc_to_procvar : tnode;
  2019. begin
  2020. result:=first_proc_to_procvar;
  2021. end;
  2022. function ttypeconvnode._first_load_smallset : tnode;
  2023. begin
  2024. result:=first_load_smallset;
  2025. end;
  2026. function ttypeconvnode._first_cord_to_pointer : tnode;
  2027. begin
  2028. result:=first_cord_to_pointer;
  2029. end;
  2030. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2031. begin
  2032. result:=first_ansistring_to_pchar;
  2033. end;
  2034. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2035. begin
  2036. result:=first_arrayconstructor_to_set;
  2037. end;
  2038. function ttypeconvnode._first_class_to_intf : tnode;
  2039. begin
  2040. result:=first_class_to_intf;
  2041. end;
  2042. function ttypeconvnode._first_char_to_char : tnode;
  2043. begin
  2044. result:=first_char_to_char;
  2045. end;
  2046. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2047. const
  2048. firstconvert : array[tconverttype] of pointer = (
  2049. nil, { none }
  2050. @ttypeconvnode._first_nothing, {equal}
  2051. @ttypeconvnode._first_nothing, {not_possible}
  2052. nil, { removed in resulttype_string_to_string }
  2053. @ttypeconvnode._first_char_to_string,
  2054. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2055. nil, { removed in resulttype_chararray_to_string }
  2056. @ttypeconvnode._first_cchar_to_pchar,
  2057. @ttypeconvnode._first_cstring_to_pchar,
  2058. @ttypeconvnode._first_cstring_to_int,
  2059. @ttypeconvnode._first_ansistring_to_pchar,
  2060. @ttypeconvnode._first_string_to_chararray,
  2061. nil, { removed in resulttype_chararray_to_string }
  2062. @ttypeconvnode._first_array_to_pointer,
  2063. @ttypeconvnode._first_pointer_to_array,
  2064. @ttypeconvnode._first_int_to_int,
  2065. @ttypeconvnode._first_int_to_bool,
  2066. @ttypeconvnode._first_bool_to_bool,
  2067. @ttypeconvnode._first_bool_to_int,
  2068. @ttypeconvnode._first_real_to_real,
  2069. @ttypeconvnode._first_int_to_real,
  2070. nil, { removed in resulttype_real_to_currency }
  2071. @ttypeconvnode._first_proc_to_procvar,
  2072. @ttypeconvnode._first_arrayconstructor_to_set,
  2073. @ttypeconvnode._first_load_smallset,
  2074. @ttypeconvnode._first_cord_to_pointer,
  2075. @ttypeconvnode._first_nothing,
  2076. @ttypeconvnode._first_nothing,
  2077. @ttypeconvnode._first_class_to_intf,
  2078. @ttypeconvnode._first_char_to_char,
  2079. @ttypeconvnode._first_nothing,
  2080. @ttypeconvnode._first_nothing,
  2081. nil,
  2082. nil,
  2083. nil,
  2084. nil,
  2085. nil,
  2086. nil,
  2087. nil,
  2088. nil
  2089. );
  2090. type
  2091. tprocedureofobject = function : tnode of object;
  2092. var
  2093. r : packed record
  2094. proc : pointer;
  2095. obj : pointer;
  2096. end;
  2097. begin
  2098. { this is a little bit dirty but it works }
  2099. { and should be quite portable too }
  2100. r.proc:=firstconvert[c];
  2101. r.obj:=self;
  2102. if not assigned(r.proc) then
  2103. internalerror(200312081);
  2104. first_call_helper:=tprocedureofobject(r)()
  2105. end;
  2106. function ttypeconvnode.pass_1 : tnode;
  2107. begin
  2108. result:=nil;
  2109. firstpass(left);
  2110. if codegenerror then
  2111. exit;
  2112. { load the value_str from the left part }
  2113. registersint:=left.registersint;
  2114. registersfpu:=left.registersfpu;
  2115. {$ifdef SUPPORT_MMX}
  2116. registersmmx:=left.registersmmx;
  2117. {$endif}
  2118. expectloc:=left.expectloc;
  2119. result:=first_call_helper(convtype);
  2120. end;
  2121. function ttypeconvnode.assign_allowed:boolean;
  2122. begin
  2123. result:=(convtype=tc_equal) or
  2124. { typecasting from void is always allowed }
  2125. is_void(left.resulttype.def) or
  2126. (left.resulttype.def.deftype=formaldef) or
  2127. { int 2 int with same size reuses same location, or for
  2128. tp7 mode also allow size < orignal size }
  2129. (
  2130. (convtype=tc_int_2_int) and
  2131. (
  2132. (resulttype.def.size=left.resulttype.def.size) or
  2133. ((m_tp7 in aktmodeswitches) and
  2134. (resulttype.def.size<left.resulttype.def.size))
  2135. )
  2136. ) or
  2137. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2138. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  2139. (nf_explicit in flags) and
  2140. (resulttype.def.size=left.resulttype.def.size));
  2141. { When using only a part of the value it can't be in a register since
  2142. that will load the value in a new register first }
  2143. if (resulttype.def.size<left.resulttype.def.size) then
  2144. make_not_regable(left,vr_addr);
  2145. end;
  2146. function ttypeconvnode.docompare(p: tnode) : boolean;
  2147. begin
  2148. docompare :=
  2149. inherited docompare(p) and
  2150. (convtype = ttypeconvnode(p).convtype);
  2151. end;
  2152. procedure ttypeconvnode._second_int_to_int;
  2153. begin
  2154. second_int_to_int;
  2155. end;
  2156. procedure ttypeconvnode._second_string_to_string;
  2157. begin
  2158. second_string_to_string;
  2159. end;
  2160. procedure ttypeconvnode._second_cstring_to_pchar;
  2161. begin
  2162. second_cstring_to_pchar;
  2163. end;
  2164. procedure ttypeconvnode._second_cstring_to_int;
  2165. begin
  2166. second_cstring_to_int;
  2167. end;
  2168. procedure ttypeconvnode._second_string_to_chararray;
  2169. begin
  2170. second_string_to_chararray;
  2171. end;
  2172. procedure ttypeconvnode._second_array_to_pointer;
  2173. begin
  2174. second_array_to_pointer;
  2175. end;
  2176. procedure ttypeconvnode._second_pointer_to_array;
  2177. begin
  2178. second_pointer_to_array;
  2179. end;
  2180. procedure ttypeconvnode._second_chararray_to_string;
  2181. begin
  2182. second_chararray_to_string;
  2183. end;
  2184. procedure ttypeconvnode._second_char_to_string;
  2185. begin
  2186. second_char_to_string;
  2187. end;
  2188. procedure ttypeconvnode._second_int_to_real;
  2189. begin
  2190. second_int_to_real;
  2191. end;
  2192. procedure ttypeconvnode._second_real_to_real;
  2193. begin
  2194. second_real_to_real;
  2195. end;
  2196. procedure ttypeconvnode._second_cord_to_pointer;
  2197. begin
  2198. second_cord_to_pointer;
  2199. end;
  2200. procedure ttypeconvnode._second_proc_to_procvar;
  2201. begin
  2202. second_proc_to_procvar;
  2203. end;
  2204. procedure ttypeconvnode._second_bool_to_int;
  2205. begin
  2206. second_bool_to_int;
  2207. end;
  2208. procedure ttypeconvnode._second_int_to_bool;
  2209. begin
  2210. second_int_to_bool;
  2211. end;
  2212. procedure ttypeconvnode._second_bool_to_bool;
  2213. begin
  2214. second_bool_to_bool;
  2215. end;
  2216. procedure ttypeconvnode._second_load_smallset;
  2217. begin
  2218. second_load_smallset;
  2219. end;
  2220. procedure ttypeconvnode._second_ansistring_to_pchar;
  2221. begin
  2222. second_ansistring_to_pchar;
  2223. end;
  2224. procedure ttypeconvnode._second_class_to_intf;
  2225. begin
  2226. second_class_to_intf;
  2227. end;
  2228. procedure ttypeconvnode._second_char_to_char;
  2229. begin
  2230. second_char_to_char;
  2231. end;
  2232. procedure ttypeconvnode._second_nothing;
  2233. begin
  2234. second_nothing;
  2235. end;
  2236. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2237. const
  2238. secondconvert : array[tconverttype] of pointer = (
  2239. @ttypeconvnode._second_nothing, {none}
  2240. @ttypeconvnode._second_nothing, {equal}
  2241. @ttypeconvnode._second_nothing, {not_possible}
  2242. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resulttype pass }
  2243. @ttypeconvnode._second_char_to_string,
  2244. @ttypeconvnode._second_nothing, {char_to_charray}
  2245. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resulttype pass }
  2246. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2247. @ttypeconvnode._second_cstring_to_pchar,
  2248. @ttypeconvnode._second_cstring_to_int,
  2249. @ttypeconvnode._second_ansistring_to_pchar,
  2250. @ttypeconvnode._second_string_to_chararray,
  2251. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resulttype pass }
  2252. @ttypeconvnode._second_array_to_pointer,
  2253. @ttypeconvnode._second_pointer_to_array,
  2254. @ttypeconvnode._second_int_to_int,
  2255. @ttypeconvnode._second_int_to_bool,
  2256. @ttypeconvnode._second_bool_to_bool,
  2257. @ttypeconvnode._second_bool_to_int,
  2258. @ttypeconvnode._second_real_to_real,
  2259. @ttypeconvnode._second_int_to_real,
  2260. @ttypeconvnode._second_nothing, { real_to_currency, handled in resulttype pass }
  2261. @ttypeconvnode._second_proc_to_procvar,
  2262. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2263. @ttypeconvnode._second_nothing, { second_load_smallset, handled in first pass }
  2264. @ttypeconvnode._second_cord_to_pointer,
  2265. @ttypeconvnode._second_nothing, { interface 2 string }
  2266. @ttypeconvnode._second_nothing, { interface 2 guid }
  2267. @ttypeconvnode._second_class_to_intf,
  2268. @ttypeconvnode._second_char_to_char,
  2269. @ttypeconvnode._second_nothing, { normal_2_smallset }
  2270. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2271. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2272. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2273. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2274. @ttypeconvnode._second_nothing, { variant_2_enum }
  2275. @ttypeconvnode._second_nothing, { enum_2_variant }
  2276. @ttypeconvnode._second_nothing, { variant_2_interface }
  2277. @ttypeconvnode._second_nothing, { interface_2_variant }
  2278. @ttypeconvnode._second_nothing { array_2_dynarray }
  2279. );
  2280. type
  2281. tprocedureofobject = procedure of object;
  2282. var
  2283. r : packed record
  2284. proc : pointer;
  2285. obj : pointer;
  2286. end;
  2287. begin
  2288. { this is a little bit dirty but it works }
  2289. { and should be quite portable too }
  2290. r.proc:=secondconvert[c];
  2291. r.obj:=self;
  2292. tprocedureofobject(r)();
  2293. end;
  2294. {*****************************************************************************
  2295. TISNODE
  2296. *****************************************************************************}
  2297. constructor tisnode.create(l,r : tnode);
  2298. begin
  2299. inherited create(isn,l,r);
  2300. end;
  2301. function tisnode.det_resulttype:tnode;
  2302. var
  2303. paras: tcallparanode;
  2304. begin
  2305. result:=nil;
  2306. resulttypepass(left);
  2307. resulttypepass(right);
  2308. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2309. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2310. if codegenerror then
  2311. exit;
  2312. if (right.resulttype.def.deftype=classrefdef) then
  2313. begin
  2314. { left must be a class }
  2315. if is_class(left.resulttype.def) then
  2316. begin
  2317. { the operands must be related }
  2318. if (not(tobjectdef(left.resulttype.def).is_related(
  2319. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2320. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2321. tobjectdef(left.resulttype.def)))) then
  2322. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  2323. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  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 if is_interface(right.resulttype.def) then
  2337. begin
  2338. { left is a class }
  2339. if is_class(left.resulttype.def) then
  2340. begin
  2341. { the operands must be related }
  2342. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  2343. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  2344. CGMessage2(type_e_classes_not_related,
  2345. FullTypeName(left.resulttype.def,right.resulttype.def),
  2346. FullTypeName(right.resulttype.def,left.resulttype.def))
  2347. end
  2348. { left is an interface }
  2349. else if is_interface(left.resulttype.def) then
  2350. begin
  2351. { the operands must be related }
  2352. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  2353. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  2354. CGMessage2(type_e_classes_not_related,
  2355. FullTypeName(left.resulttype.def,right.resulttype.def),
  2356. FullTypeName(right.resulttype.def,left.resulttype.def));
  2357. end
  2358. else
  2359. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2360. { call fpc_do_is helper }
  2361. paras := ccallparanode.create(
  2362. left,
  2363. ccallparanode.create(
  2364. right,nil));
  2365. result := ccallnode.createintern('fpc_do_is',paras);
  2366. left := nil;
  2367. right := nil;
  2368. end
  2369. else
  2370. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2371. resulttype:=booltype;
  2372. end;
  2373. function tisnode.pass_1 : tnode;
  2374. begin
  2375. internalerror(200204254);
  2376. result:=nil;
  2377. end;
  2378. { dummy pass_2, it will never be called, but we need one since }
  2379. { you can't instantiate an abstract class }
  2380. procedure tisnode.pass_2;
  2381. begin
  2382. end;
  2383. {*****************************************************************************
  2384. TASNODE
  2385. *****************************************************************************}
  2386. constructor tasnode.create(l,r : tnode);
  2387. begin
  2388. inherited create(asn,l,r);
  2389. call := nil;
  2390. end;
  2391. destructor tasnode.destroy;
  2392. begin
  2393. call.free;
  2394. inherited destroy;
  2395. end;
  2396. function tasnode.det_resulttype:tnode;
  2397. var
  2398. hp : tnode;
  2399. begin
  2400. result:=nil;
  2401. resulttypepass(right);
  2402. resulttypepass(left);
  2403. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2404. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2405. if codegenerror then
  2406. exit;
  2407. if (right.resulttype.def.deftype=classrefdef) then
  2408. begin
  2409. { left must be a class }
  2410. if is_class(left.resulttype.def) then
  2411. begin
  2412. { the operands must be related }
  2413. if (not(tobjectdef(left.resulttype.def).is_related(
  2414. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2415. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2416. tobjectdef(left.resulttype.def)))) then
  2417. CGMessage2(type_e_classes_not_related,
  2418. FullTypeName(left.resulttype.def,tclassrefdef(right.resulttype.def).pointertype.def),
  2419. FullTypeName(tclassrefdef(right.resulttype.def).pointertype.def,left.resulttype.def));
  2420. end
  2421. else
  2422. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2423. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  2424. end
  2425. else if is_interface(right.resulttype.def) then
  2426. begin
  2427. { left is a class }
  2428. if not(is_class(left.resulttype.def) or
  2429. is_interfacecom(left.resulttype.def)) then
  2430. CGMessage1(type_e_class_or_cominterface_type_expected,left.resulttype.def.typename);
  2431. resulttype:=right.resulttype;
  2432. { load the GUID of the interface }
  2433. if (right.nodetype=typen) then
  2434. begin
  2435. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  2436. begin
  2437. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  2438. right.free;
  2439. right:=hp;
  2440. end
  2441. else
  2442. internalerror(200206282);
  2443. resulttypepass(right);
  2444. end;
  2445. end
  2446. else
  2447. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2448. end;
  2449. function tasnode._getcopy: tnode;
  2450. begin
  2451. result := inherited _getcopy;
  2452. if assigned(call) then
  2453. tasnode(result).call := call.getcopy
  2454. else
  2455. tasnode(result).call := nil;
  2456. end;
  2457. function tasnode.pass_1 : tnode;
  2458. var
  2459. procname: string;
  2460. begin
  2461. result:=nil;
  2462. if not assigned(call) then
  2463. begin
  2464. if is_class(left.resulttype.def) and
  2465. (right.resulttype.def.deftype=classrefdef) then
  2466. call := ccallnode.createinternres('fpc_do_as',
  2467. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2468. resulttype)
  2469. else
  2470. begin
  2471. if is_class(left.resulttype.def) then
  2472. procname := 'fpc_class_as_intf'
  2473. else
  2474. procname := 'fpc_intf_as';
  2475. call := ccallnode.createintern(procname,
  2476. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2477. call := ctypeconvnode.create_internal(call,resulttype);
  2478. end;
  2479. left := nil;
  2480. right := nil;
  2481. firstpass(call);
  2482. if codegenerror then
  2483. exit;
  2484. expectloc:=call.expectloc;
  2485. registersint:=call.registersint;
  2486. registersfpu:=call.registersfpu;
  2487. {$ifdef SUPPORT_MMX}
  2488. registersmmx:=call.registersmmx;
  2489. {$endif SUPPORT_MMX}
  2490. end;
  2491. end;
  2492. begin
  2493. ctypeconvnode:=ttypeconvnode;
  2494. casnode:=tasnode;
  2495. cisnode:=tisnode;
  2496. end.