ncnv.pas 106 KB

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