ncnv.pas 105 KB

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