ncnv.pas 104 KB

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