ncnv.pas 107 KB

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