ncnv.pas 106 KB

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